package com.hspedu.newData.dmsxl.哈希表;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-27 
 * @Version: 1.0
 */

public class LC219存在重复元素Ⅱ {

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> map = new HashMap<>();

        int count = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {

            if (map.containsKey(nums[i])){
                count = Math.min(count,i - map.get(nums[i]));
                if (count <= k){
                    return true;
                }
            }
            map.put(nums[i],i);
        }
        return false;
    }

}
